package com.zhang.yygh.hosp.controller.admin;

import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.yygh.common.exception.YYGHException;
import com.zhang.yygh.common.result.R;
import com.zhang.yygh.hosp.service.HospitalSetService;
import com.zhang.yygh.hosp.utils.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-02-28
 */
@Api(tags = "管理员系统医院设置接口文档")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /*//查询列表
    @ApiOperation(value = "查询所有的医院设置列表")
    @GetMapping("/list")
    public List<HospitalSet> getHospitalSetList(){
        List<HospitalSet> list = hospitalSetService.list();
        return list;
    }*/

    @ApiOperation(value = "修改医院设置信息")
    @PutMapping("/modify/{id}/{status}")
    public R modifyStatus(@PathVariable Long id,
                          @PathVariable Integer status){
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public R batchDelete(
            @ApiParam(name = "ids", value = "批量删除的id值", required = true)
            @RequestBody List<Long> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    @ApiOperation(value = "修改医院设置信息")
    @PostMapping("/update")
    public R updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }


    @ApiOperation(value = "根据id获取医院设置信息")
    @GetMapping("/info/{id}")
    public R saveHospitalSet(@PathVariable Long id){
        /*//针对自定义异常，必须手动抛出
        try {
            int result=10/0;
        } catch (Exception ex) {
            throw new YYGHException(20001,"预约失败");
        }*/
        HospitalSet item = hospitalSetService.getById(id);
        return R.ok().data("item",item);
    }


    @ApiOperation(value = "添加--保存医院设置信息")
    @PostMapping("/save")
    public R saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "带查询条件的医院设置分页信息")
    @PostMapping("{page}/{limit}")
    public R getHospitalSetPage(
                                @ApiParam(name = "page", value = "当前页码",required = true)
                                @PathVariable long page,
                                @ApiParam(name = "limit", value = "每页记录数",required = true)
                                @PathVariable long limit,
                                @ApiParam(name = "hospitalSetQueryVo", value = "查询对象", required = false)
                                @RequestBody HospitalQueryVo hospitalQueryVo){
        Page pageParam=new Page(page,limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hospitalQueryVo.getHosname())){
            queryWrapper.like("hosname",hospitalQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getHoscode())){
            queryWrapper.eq("hoscode",hospitalQueryVo.getHoscode());
        }
        hospitalSetService.page(pageParam, queryWrapper);
        return R.ok().data("total",pageParam.getTotal()).data("rows",pageParam.getRecords());
    }

    //查询列表
    @ApiOperation(value = "查询所有的医院设置列表")
    @GetMapping("/list")
    public R getHospitalSetList(){
        return R.ok().data("list",hospitalSetService.list());
    }

    //逻辑删除
    @ApiOperation(value = "根据医院设置id删除医院设置信息")
    @DeleteMapping("/delete/{id}")
    public R deleteById(
            @ApiParam(name = "id",value = "医院设置id",required = true)
            @PathVariable Long id){
        hospitalSetService.removeById(id);
        return R.ok();
    }
}

